﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using YYSite.Application;
using YYSite.Common;
using Microsoft.Extensions.Logging;
using YYSite.Domain.System.IdentityUsers;
using HtmlAgilityPack;
using YYSite.Application.IdentityUsers;
using YYSite.Application.System.IdentityUsers;

namespace YYSite.Controllers
{
    public class HomeController : Controller
    {
        public HomeController(IIdentityUserService identityUserService, ILoggerFactory loggerFactory)
        {
            IdentityUserService = identityUserService;
            Logger = loggerFactory.CreateLogger(this.GetType());
        }

        private IIdentityUserService IdentityUserService { get; }
        private  ILogger Logger { get; }

      
        public IActionResult Index()
        {
            Logger.LogInformation("aaaaaa");
            //HtmlWeb webClient = new HtmlWeb();
            //HtmlDocument doc = webClient.Load("http://www.qmsjmfb.com/");

            //HtmlNodeCollection nodes = doc.DocumentNode.SelectNodes("//*[@id=\"cn\"]/div[5]/ul/li");
            //Random rd = new Random();
       
            //foreach (HtmlNode node in nodes)
            //{

            //    IdentityUser u = new IdentityUser();
            //    u.NickName = node.InnerText.Trim();
            //    u.UserName = PinYinUtility.Convert(u.NickName);
            //    u.UserName = u.UserName +rd.Next(100,999);
             
            //    IdentityUserRepository.Create(u);
            //}
            //IdentityUserRepository.Flush();

            return View();
        }

        public IActionResult Blank()
        {
            ViewData["Message"] = "Your application description page.";

            return View();
        }

    
        public DataGridOutput<IdentityUser> GetBlankData(GetIdentityUserDataGridInput input)
        {
            List<dynamic> list = new List<dynamic>();
            for (int i = 0; i < 10; i++)
            {
                list.Add(new { id = Guid.NewGuid(), name = "夫子" + i, createTime = DateTime.Now });
            }
           // return new { total = 100, rows = list };
         return this.IdentityUserService.GetIdentityUserByPage(input);
            // return new JsonResult(new { total = 100, rows = list });
        }

        public IActionResult Contact()
        {
            ViewData["Message"] = "Your contact page.";

            return View();
        }

        public IActionResult Error()
        {
            return View();
        }

        public DateTime ToDateTime(object value)
        {
            DateTime result = DateTime.MinValue;
            if(value!=null)
                DateTime.TryParse(value.ToString(),out result);
            return result;
        }

        public IActionResult GetImage()
        {
           

            throw new BadImageFormatException();
        }
    }
}
